{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "7be8b4e2a8e853ba",
   "metadata": {},
   "source": [
    "# 2.3.6.1. 非降维求和"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "f6d79ad6cbbc9fb0",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-02-14T03:20:32.956884Z",
     "start_time": "2025-02-14T03:20:25.010850Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "tensor([[ 0.,  1.,  2.,  3.],\n",
       "        [ 4.,  5.,  6.,  7.],\n",
       "        [ 8.,  9., 10., 11.],\n",
       "        [12., 13., 14., 15.],\n",
       "        [16., 17., 18., 19.]])"
      ]
     },
     "execution_count": 1,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "import torch\n",
    "A = torch.arange(20, dtype=torch.float32).reshape(5, 4)\n",
    "A"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "initial_id",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-02-14T03:24:27.642482Z",
     "start_time": "2025-02-14T03:24:27.615076Z"
    },
    "collapsed": true,
    "jupyter": {
     "outputs_hidden": true
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "tensor([[ 6.],\n",
       "        [22.],\n",
       "        [38.],\n",
       "        [54.],\n",
       "        [70.]])"
      ]
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "sum_A = A.sum(axis=1, keepdims=True)\n",
    "sum_A"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "ddcbe091c67f749f",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-02-14T03:25:59.780832Z",
     "start_time": "2025-02-14T03:25:59.755835Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "tensor([[0.0000, 0.1667, 0.3333, 0.5000],\n",
       "        [0.1818, 0.2273, 0.2727, 0.3182],\n",
       "        [0.2105, 0.2368, 0.2632, 0.2895],\n",
       "        [0.2222, 0.2407, 0.2593, 0.2778],\n",
       "        [0.2286, 0.2429, 0.2571, 0.2714]])"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "A / sum_A"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "156915193815a9bc",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-02-14T03:33:47.191541Z",
     "start_time": "2025-02-14T03:33:47.149543Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "tensor([[ 0.,  1.,  2.,  3.],\n",
       "        [ 4.,  6.,  8., 10.],\n",
       "        [12., 15., 18., 21.],\n",
       "        [24., 28., 32., 36.],\n",
       "        [40., 45., 50., 55.]])"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "A.cumsum(axis=0)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "db14f1cbd9f3f8a6",
   "metadata": {},
   "source": [
    "# 2.3.7. 点积（Dot Product）\n",
    "![](./矩阵点积.jpg)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "183f0562ef29f364",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-02-14T03:35:16.457838Z",
     "start_time": "2025-02-14T03:35:16.434804Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(tensor([0., 1., 2., 3.]), tensor([1., 1., 1., 1.]), tensor(6.))"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "x = torch.arange(4, dtype=torch.float32)\n",
    "y = torch.ones(4, dtype = torch.float32)\n",
    "x, y, torch.dot(x, y)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "9547d4fe4fcee8d2",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-02-14T03:36:32.720716Z",
     "start_time": "2025-02-14T03:36:32.697716Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "tensor(6.)"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "torch.sum(x * y)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "161dd9717866df17",
   "metadata": {},
   "source": [
    "# 2.3.10. 范数"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "39a7479aaba9d8a5",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-02-14T04:32:35.645087Z",
     "start_time": "2025-02-14T04:32:35.616048Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "tensor(5.)"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# L2范数\n",
    "u = torch.tensor([3.0, -4.0])\n",
    "torch.norm(u)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "b1a6cc2e291d7905",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-02-14T05:24:15.114908Z",
     "start_time": "2025-02-14T05:24:15.092910Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "tensor(7.)"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# L1范数\n",
    "torch.abs(u).sum()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "f89e3576e4ad4c4c",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-02-14T05:26:36.117370Z",
     "start_time": "2025-02-14T05:26:36.099341Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "tensor(6.)"
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 矩阵的范数(Frobenius范数)\n",
    "torch.norm(torch.ones((4, 9)))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "f01d924a319892d7",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-02-14T06:32:47.291913Z",
     "start_time": "2025-02-14T06:32:47.268668Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "1"
      ]
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "## 查看有几个维度\n",
    "u.ndim"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "7b54f54c3928c76f",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-02-14T07:03:55.638311Z",
     "start_time": "2025-02-14T07:03:55.621280Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['C:\\\\Users\\\\17634\\\\Desktop\\\\Dive_into_Deep_Learning',\n",
       " 'C:\\\\Users\\\\17634\\\\miniconda3\\\\envs\\\\torch\\\\python310.zip',\n",
       " 'C:\\\\Users\\\\17634\\\\miniconda3\\\\envs\\\\torch\\\\DLLs',\n",
       " 'C:\\\\Users\\\\17634\\\\miniconda3\\\\envs\\\\torch\\\\lib',\n",
       " 'C:\\\\Users\\\\17634\\\\miniconda3\\\\envs\\\\torch',\n",
       " '',\n",
       " 'C:\\\\Users\\\\17634\\\\miniconda3\\\\envs\\\\torch\\\\lib\\\\site-packages',\n",
       " 'C:\\\\Users\\\\17634\\\\miniconda3\\\\envs\\\\torch\\\\lib\\\\site-packages\\\\win32',\n",
       " 'C:\\\\Users\\\\17634\\\\miniconda3\\\\envs\\\\torch\\\\lib\\\\site-packages\\\\win32\\\\lib',\n",
       " 'C:\\\\Users\\\\17634\\\\miniconda3\\\\envs\\\\torch\\\\lib\\\\site-packages\\\\Pythonwin']"
      ]
     },
     "execution_count": 1,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "import sys\n",
    "sys.path"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "ce80aa1fba6ddb1b",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.10.12"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
